package main

import (
	"bufio"
	"fmt"
	"os"
	"strconv"

	"gitee.com/fcsvr/fc/fclog"
)

func ConstructAlphaExpressToFile() {
	ConstructSuperExpressToFile()
	//ConstructAlphaExpressToFile1()
	//ConstructAlphaExpressToFile2()
	//ConstructAlphaExpressToFile3()
	//ConstructAlphaExpressToFile4()
	//ConstructAlphaExpressToFile5()
	//ConstructAlphaExpressToFile6()
}

func ConstructBaseOp(base string) string {
	line := ""
	for i := 0; i < 4; i++ {
		line = line + G_BasicOp[i] + "(" + base + ");" + "\n"
	}
	line += fmt.Sprintf("normalize(%s, useStd = false, limit = 0.0)\n", base)
	line += fmt.Sprintf("quantile(%s, driver = gaussian, sigma = 1.0)\n", base)
	return line
}

func ConstructTsOp(base string) string {
	line := ""

	for _, v := range G_Day {
		line += fmt.Sprintf("ts_rank(%s, %s)\n", base, v)
		line += fmt.Sprintf("ts_zscore(%s, %s)\n", base, v)
		line += fmt.Sprintf("ts_delta(%s, %s)\n", base, v)
		line += fmt.Sprintf("ts_delay(%s, %s)\n", base, v)
		line += fmt.Sprintf("ts_sum(%s, %s)\n", base, v)
		line += fmt.Sprintf("ts_product(%s, %s)\n", base, v)
		line += fmt.Sprintf("ts_std_dev(%s, %s)\n", base, v)
		line += fmt.Sprintf("ts_mean(%s, %s)\n", base, v)
		line += fmt.Sprintf("ts_arg_min(%s, %s)\n", base, v)
		line += fmt.Sprintf("ts_scale(%s, %s)\n", base, v)
		line += fmt.Sprintf("ts_quantile(%s, %s)\n", base, v)
	}

	return line
}

func ConstructAlphaExpressToFile1() {
	//CHN单个数据
	//%s(%s, densify(industry))
	//%s(%s/%s, densify(industry))

	region := "EUR"
	var filePath string = "file/" + region + "/1"
	index := 5
	count := 0
	path := filePath + "-" + strconv.Itoa(index)
	file, _ := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
	defer file.Close()
	writer := bufio.NewWriter(file)

	G_AlphaExp[1] = "@ %s(%s, %s);"

	for _, v := range SET2 {
		fclog.Debug("%v", v)
		fields := GetDataField(v, "EQUITY", region, "1", G_Universe[region][0], "MATRIX")
		for _, v1 := range G_TsOp {
			for _, v2 := range fields {
				for _, v3 := range G_Day {
					count++
					str := fmt.Sprintf(G_AlphaExp[1], v1, v2, v3) + " @4 @INDUSTRY" + " @" + region + " @" + G_Universe[region][0] + "\n"
					writer.WriteString(str)
					if count == 100000 {
						index++
						count = 0
						writer.Flush()
						file.Close()
						path = filePath + "-" + strconv.Itoa(index)
						file, _ := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
						writer = bufio.NewWriter(file)
					}
				}
			}
			writer.Flush()
		}
	}
	/*
		G_AlphaExp[1] = "%s(%s/%s, densify(industry));"
		for _, v := range G_CHN_SET {
			fields := GetDataField(v, "EQUITY", "CHN", "1", "TOP2000U", "MATRIX")
			for _, v1 := range G_TsOp {
				for _, v2 := range fields {
					for _, v3 := range fields {
						if v2 == v3 {
							continue
						}
						count++
						str := fmt.Sprintf(G_AlphaExp[1], v1, v2, v3) + " @4 @INDUSTRY\n"
						writer.WriteString(str)
						if count == 100000 {
							index++
							count = 0
							writer.Flush()
							file.Close()
							path := filePath + "-" + strconv.Itoa(index)
							file, _ := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
							writer = bufio.NewWriter(file)
						}
					}
					writer.Flush()
				}
			}
		}
	*/
	file.Close()
}

func ConstructAlphaExpressToFile2() {
	G_AlphaExp[2] = "@ sentiment = ts_backfill(ts_delay(%s,1),20);vhat=ts_regression(%s,sentiment,%s);alpha=%s(vhat,bucket(rank(cap),range='0,1,0.1')); @%s\n"
	//实时从url中取数据然后构造
	//"group_rank(%s/%s, densify(industry))"
	var filePath string = "file/2"
	file, _ := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
	defer file.Close()
	writer := bufio.NewWriter(file)

	index := 0
	fields1 := GetDataField("sentiment1", "EQUITY", "USA", "1", "TOP3000", "MATRIX")
	fields2 := GetDataField("pv1", "EQUITY", "USA", "1", "TOP3000", "MATRIX")
	fields3 := []string{
		"120", "250", "500",
	}
	fields5 := G_GroupOp
	fields6 := G_DecayStr
	for _, v6 := range fields6 {
		for _, v1 := range fields1 {
			for _, v2 := range fields2 {
				for _, v3 := range fields3 {
					for _, v5 := range fields5 {
						str := fmt.Sprintf(G_AlphaExp[2], v1, v2, v3, v5, v6)
						writer.WriteString(str)
						index++
					}
				}
			}
		}
	}

	writer.Flush()
}

func ConstructAlphaExpressToFile3() {
	G_AlphaExp[3] = "@ news=ts_backfill(ts_delay(%s,1),20);vhat=ts_regression(%s,news,%s);alpha=%s(vhat,bucket(rank(cap),range='0,1,0.1')); @%s\n"
	//实时从url中取数据然后构造
	//"group_rank(%s/%s, densify(industry))"
	var filePath string = "file/3"
	file, _ := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
	defer file.Close()
	writer := bufio.NewWriter(file)

	index := 0
	fields1 := GetDataField("news12", "EQUITY", "USA", "1", "TOP3000", "MATRIX")
	fields2 := GetDataField("pv1", "EQUITY", "USA", "1", "TOP3000", "MATRIX")
	fields3 := []string{
		"10", "20", "120",
	}
	fields5 := G_GroupOp
	fields6 := G_DecayStr
	expMap := make(map[int]string)
	for _, v6 := range fields6 {
		for _, v1 := range fields1 {
			for _, v2 := range fields2 {
				for _, v3 := range fields3 {
					for _, v5 := range fields5 {
						str := fmt.Sprintf(G_AlphaExp[3], v1, v2, v3, v5, v6)
						expMap[index] = str
						index++
					}
				}
			}
		}
	}
	for _, v := range expMap {
		writer.WriteString(v)
	}

	writer.Flush()
}

func ConstructAlphaExpressToFile4() {
	G_AlphaExp[4] = "@ ts_regression (ts_zscore(%s,500), ts_zscore(%s,500),500) @%s\n"
	//实时从url中取数据然后构造
	//"group_rank(%s/%s, densify(industry))"
	var filePath string = "file/4"
	file, _ := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
	defer file.Close()
	writer := bufio.NewWriter(file)

	index := 0
	fields1 := G_DecayStr
	fields2 := GetDataField("option8", "EQUITY", "USA", "1", "TOP3000", "MATRIX")
	fields3 := GetDataField("pv1", "EQUITY", "USA", "1", "TOP3000", "MATRIX")
	fields4 := GetDataField("option9", "EQUITY", "USA", "1", "TOP3000", "MATRIX")
	expMap := make(map[int]string)
	for _, v1 := range fields1 {
		for _, v2 := range fields2 {
			for _, v3 := range fields3 {
				str := fmt.Sprintf(G_AlphaExp[4], v2, v3, v1)
				expMap[index] = str
				index++
			}
		}
	}
	for _, v1 := range fields1 {
		for _, v4 := range fields4 {
			for _, v3 := range fields3 {
				str := fmt.Sprintf(G_AlphaExp[4], v4, v3, v1)
				expMap[index] = str
				index++
			}
		}
	}
	for _, v := range expMap {
		writer.WriteString(v)
	}
	writer.Flush()
}

func ConstructAlphaExpressToFile5() {
	G_AlphaExp[5] = "@ ts_regression (ts_zscore(%s,500), ts_zscore(%s,500),500) @%s\n"
	//实时从url中取数据然后构造
	//"group_rank(%s/%s, densify(industry))"
	var filePath string = "file/5"
	file, _ := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
	defer file.Close()
	writer := bufio.NewWriter(file)

	index := 0
	fields1 := G_DecayStr
	fields4 := GetDataField("model77", "EQUITY", "USA", "1", "TOP3000", "MATRIX")
	fields3 := GetDataField("pv1", "EQUITY", "USA", "1", "TOP3000", "MATRIX")
	fields2 := GetDataField("model53", "EQUITY", "USA", "1", "TOP3000", "MATRIX")
	expMap := make(map[int]string)
	for _, v1 := range fields1 {
		for _, v2 := range fields2 {
			for _, v3 := range fields3 {
				str := fmt.Sprintf(G_AlphaExp[4], v2, v3, v1)
				expMap[index] = str
				index++
			}
		}
	}
	for _, v1 := range fields1 {
		for _, v4 := range fields4 {
			for _, v3 := range fields3 {
				str := fmt.Sprintf(G_AlphaExp[4], v4, v3, v1)
				expMap[index] = str
				index++
			}
		}
	}
	for _, v := range expMap {
		writer.WriteString(v)
	}
	writer.Flush()
}

func ConstructAlphaExpressToFile6() {
	alphaExp := "@ group_rank(%s/cap, densify(INDUSTRY)); @0 @INDUSTRY \n"
	//实时从url中取数据然后构造
	var filePath string = "file/6"
	file, _ := os.OpenFile(filePath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
	defer file.Close()
	writer := bufio.NewWriter(file)

	index := 0
	fields1 := GetDataField("fundamental7", "EQUITY", "USA", "1", "TOP3000", "MATRIX")
	//fields2 := GetDataField("fundamental7", "EQUITY", "USA", "1", "TOP3000", "MATRIX")
	expMap := make(map[int]string)
	for _, v1 := range fields1 {
		str := fmt.Sprintf(alphaExp, v1)
		expMap[index] = str
		index++
	}
	for _, v := range expMap {
		writer.WriteString(v)
	}
	writer.Flush()
}

func ConstructSuperExpressToFile() {
	//CHN单个数据
	//%s(%s, densify(industry))
	//%s(%s/%s, densify(industry))

	//region := "USA"
	var filePath string = "file/SUPER" + "/USA"
	index := 1
	count := 0
	path := filePath + "-" + strconv.Itoa(index)
	file, _ := os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
	defer file.Close()

	superExp := "@%s @%s @%s @%d @%s @%s @%s\n"

	for _, region := range G_Region {
		index = 1
		count = 0
		filePath = "file/SUPER/" + region
		path = filePath + "-" + strconv.Itoa(index)
		file, _ = os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
		writer := bufio.NewWriter(file)
		for _, v2 := range G_SuperSelectionLimit {
			for _, v3 := range G_SuperSelectionHandle {
				for _, v4 := range G_Universe[region] {
					for _, v5 := range G_Neutra[region] {
						for _, v := range G_SuperSelection {
							for _, v1 := range G_SuperCombo {
								count++
								str := fmt.Sprintf(superExp, region, v, v1, v2, v3, v4, v5)
								writer.WriteString(str)
								if count == 100000 {
									index++
									count = 0
									writer.Flush()
									//file.Close()
									path = filePath + "-" + strconv.Itoa(index)
									file, _ = os.OpenFile(path, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, 0600)
									writer = bufio.NewWriter(file)
								}
							}
						}
					}
				}
			}
			writer.Flush()
		}
	}
	file.Close()
}
